<?php

// *** Documentation
/*______________________________________________________________________________

  Bafflegate Site Metric Script
  Version 0.01
  
  Last Update: August 2007
  Author: Tom at klenwell@gmail.com
  
  SUMMARY
  pseudo-code outline of basic logging routine
  
  NOTES
  this is is psedo-code and not intended for execution
		
______________________________________________________________________________*/


// *** Pre-Script (core settings, includes)
/*____________________________________________________________________________*/

// ** Meta Variables
	
  // timer
  $_tx1 = microtime();
  
  // syntax declarations
  define('DS', DIRECTORY_SEPARATOR);
  
  // pre-script events
  if ( ob_get_level() == 0 ) ob_start();
  session_start();
	

// ** Core Page Settings
#$_DIR['super'] = dirname($_SERVER['DOCUMENT_ROOT']) . DS;
#$_DIR['root'] = dirname($_SERVER['PHP_SELF']) . DS;

	
// ** Includes
//require_once($_DIR['root'] . 'file.inc.php');
	
/*============================================================================*/



// *** Data (triggers, flags, HTML, and other variables)
/*____________________________________________________________________________*/

// ** Controller Flags

  // Triggers (Page Events)
  $_TRIGGER['cx_baffler'] = 0;
  $_TRIGGER['log_visit'] = 0;
  $_TRIGGER['log_session'] = 0;
  $_TRIGGER['log_seo'] = 0; 
  $_TRIGGER['log_referrer'] = 0;
  $_TRIGGER['log_visitor'] = 0;


// ** Data
	

// ** Output


/*============================================================================*/



// *** Controllers (handles requests and sets event triggers)
/*____________________________________________________________________________*/

// GET Triggers
// this script will be called either from a javascript file or an img tag of 
// the page and thus should require some kind of GET value to trigger construction
// of the baffler object
if ( !empty($_GET[$cue]) )
{
  $_TRIGGER['cx_baffler'] = 1;
  $_TRIGGER['log_visit'] = 1;
}
else
{
  // can we assume that this is an error or an unwanted direct call to this file?
  $_TRIGGER['redirect'] = 1;
}

echo '<script type="text/javascript">alert("'. $_SERVER['PHP_SELF'] . ' called")</script>';
die();

/*============================================================================*/



// *** Events (trigger-controlled events)
/*____________________________________________________________________________*/

// construct object
if ( $_TRIGGER['cx_baffler'] )
{
  $Baffler = new Bafflegate();
  $Baffler->get_visitor_data();
    // get visitor data
    #client_ip
    #hostname    
    #proxy_ip
    #proxy
    #proxy_hostname
    
    #agent string
    #agent_hash
    #js_enabled
    #browser
    #version
    #os
    #screen_width
    #screen_height
    #color_res
    
    // get proxy
    // client_ip = get_ip
    // hostname = @getHostByAddr($client_ip)
    // if proxy
       // proxy_ip = get_ip_simple else client_ip
       // proxy_hostname = @getHostByAddr($proxy_ip)
}

// log visit
if ( $_TRIGGER['log_visit'] ) 
{
  // log visit
  $Baffler->log_visit();
    // apply filters -- my first impulse was to even logged filtered visitor (e.g.
      // a blacklisted ip address), but maybe safer just to ignore
      // would it be possible for script to serve as a bouncer using javascript?
      // well, bad guys probably won't have js enabled.  outside scope, anyway. 
    // get site id (referer?)
    // hit db
    // new session?  set log_session trigger
}

// log session
if ( $_TRIGGER['log_session'] ) 
{
  // log session
  $Baffler->log_session();
    // is filtered
    // hit db
    // new referral? set log_referrer
    // new search referral? set log_seo
    // new visitor? set log_visitor
}

// log referrer
if ( $_TRIGGER['log_referrer'] ) {}

// log seo
if ( $_TRIGGER['log_seo'] ) {}

// log visitor
if ( $_TRIGGER['log_visitor'] ) {}

/*============================================================================*/


// *** Output (handles requests and sets event triggers)
/*____________________________________________________________________________*/


/*============================================================================*/



// *** Post-Script (close HTML, flush buffer, and other backend operations)
/*____________________________________________________________________________*/

// ** Flush Buffer
while (ob_get_level() > 0) { ob_end_flush(); }
flush();
$_HTML['flush'] = "<!-- buffer flushed at " . date('r') . " -->";

// ** Page Timer
$_tx2 = microtime();
$_tx3 = number_format(((substr($_tx2,0,9)) + (substr($_tx2,-10)) - (substr($_tx1,0,9)) - (substr($_tx1,-10))),4); 
$_HTML['timer'] = "\n<!-- script [id: {$_PAGE['id']}] generated in $_tx3 s -->";

/*============================================================================*/
// * That's All Folks!
?>
